clear
est drop _all

* mainpath should be set to the Replication package base directory
global mainpath ".."


* output gap
import delimited "$mainpath\data\output_gap.csv", clear
gen Date = date(date,"YMD")
gen yq = qofd(Date)
rename gdpc1_gdppot output_gap
keep if yq>=yq(1985,1) & yq<yq(2023,2)
sum output_gap, d
gen weak = output_gap < `r(p50)' if ~mi(output_gap)
keep yq output_gap weak
tempfile gap
save `gap',replace

* MP surprises daily
import excel "$mainpath\data\fomc_bauer_swanson.xlsx", sheet("FOMC Announcements") firstrow clear
destring MP* TNOTE*, replace force
collapse (sum) MP* Unscheduled TNOTE*, by(Date)
drop if mi(Date)
tempfile mps
save `mps', replace

* GSW yields
import excel "$mainpath\data\feds200805.xlsx", sheet("Yields") firstrow clear
gen d=date(Date,"YMD")
drop Date
rename d Date
gen ym=mofd(Date)
gen yq=qofd(Date)
format ym %tm
format yq %tq
drop if ym==. 

destring TIPS*, force replace
destring BKEVEN*, force replace
drop if TIPSY10==.
drop BETA* TAU*

tempfile tips
save `tips', replace

import delimited "$mainpath\data\feds200628.csv", varnames(10) stringcols(1)  case(preserve) clear
gen d=date(Date,"YMD")
drop Date
rename d Date
gen ym=mofd(Date)
gen yq=qofd(Date)
format ym %tm
format yq %tq
drop if ym==.

destring SVENY* SVENF*, force replace 
drop if SVENY10==.

merge 1:1 Date using `tips'
drop _merge

merge m:1 yq using `gap'
drop _merge

drop if mi(Date)
merge 1:1 Date using `mps'
drop _merge

sort Date
gen n = _n
tsset n

* 2-day changes from t-1 to t+1
gen fs2_SVENY05 = f.SVENY05-l.SVENY05
gen fs2_SVENF05 = f.SVENF05-l.SVENF05
gen fs2_TIPSY05 = f.TIPSY05-l.TIPSY05
gen fs2_TIPSF05 = f.TIPSF05-l.TIPSF05

gen fs2_SVENY10 = f.SVENY10-l.SVENY10
gen fs2_SVENF10 = f.SVENF10-l.SVENF10
gen fs2_TIPSY10 = f.TIPSY10-l.TIPSY10
gen fs2_TIPSF10 = f.TIPSF10-l.TIPSF10

gen fs2_SVENY02 = f.SVENY02-l.SVENY02

tab weak
gen fs2_SVENY02_X_weak = fs2_SVENY02 * weak

* Match HS sample
gen sample = ~mi(MPS) & Date>=mdy(1,1,1999) & ~inlist(Date, mdy(3,18,2009), mdy(8,10,2010), mdy(9,21,2010),mdy(11,3,2010),mdy(9,21,2011)) & ~mi(fs2_SVENY02, fs2_SVENY05, fs2_SVENF05,fs2_TIPSY05, fs2_TIPSY05,fs2_SVENY10, fs2_SVENF10,fs2_TIPSY10, fs2_TIPSF10)

local i 1
reg fs2_SVENY05 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_SVENY05 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'
reg fs2_SVENF05 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_SVENF05 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'
reg fs2_TIPSY05 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_TIPSY05 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'
reg fs2_TIPSF05 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_TIPSF05 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'

esttab m* using "$mainpath\tables\hs_a.tex", star(* .10 ** .05 *** .01)  b(2) se(2) nomtitle replace r2(2) noobs
est drop _all

local i 1
reg fs2_SVENY10 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_SVENY10 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'
reg fs2_SVENF10 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_SVENF10 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'
reg fs2_TIPSY10 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_TIPSY10 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'
reg fs2_TIPSF10 fs2_SVENY02  if sample, ro
est store m`i++'
reg fs2_TIPSF10 fs2_SVENY02 weak fs2_SVENY02_X_weak  if sample, ro
est store m`i++'

esttab m* using "$mainpath\tables\hs_b.tex", star(* .10 ** .05 *** .01)  b(2) se(2) nomtitle replace r2(2) noobs
est drop _all
